package Java8;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.util.Base64;

public class Java8LocalDate {

  public static void main(String args[]) {
    Java8LocalDate java8tester = new Java8LocalDate();
    java8tester.testLocalDateTime();
  }

  public void testLocalDateTime() {
    // Get the current date and time
    LocalDateTime currentTime = LocalDateTime.now();
    System.out.println("Current DateTime: " + currentTime);

    LocalDate date1 = currentTime.toLocalDate();
    System.out.println("date1: " + date1);

    Month month = currentTime.getMonth();
    int day = currentTime.getDayOfMonth();
    int seconds = currentTime.getSecond();

    System.out.println("Month: " + month + ",day: " + day + ",seconds: " + seconds);

    LocalDateTime date2 = currentTime.withDayOfMonth(10).withYear(2012);
    System.out.println("date2: " + date2);

    //12 december 2014
    LocalDate date3 = LocalDate.of(2014, Month.DECEMBER, 12);
    System.out.println("date3: " + date3);

    //22 hour 15 minutes
    LocalTime date4 = LocalTime.of(22, 15);
    System.out.println("date4: " + date4);

    //parse a string
    LocalTime date5 = LocalTime.parse("20:15:30");
    System.out.println("date5: " + date5);

    try {
      String base64encodedString = Base64.getEncoder().encodeToString("TutorialsPoint?java8".getBytes("utf-8"));
      System.out.println("Base64 Encoded String (Basic) :" + base64encodedString);
    } catch (Exception e) {
      e.printStackTrace();
    }

    try {
      // 编码
      String asB64 = Base64.getEncoder().encodeToString("some string".getBytes("utf-8"));
      System.out.println(asB64);
      // 输出为: c29tZSBzdHJpbmc=
      // 解码
      byte[] asBytes = Base64.getDecoder().decode("c29tZSBzdHJpbmc=");
      System.out.println(new String(asBytes, "utf-8"));
      // 输出为: some string

      String basicEncoded = Base64.getEncoder().encodeToString("subjects?abcd".getBytes("utf-8"));
      System.out.println("Using Basic Alphabet: " + basicEncoded);


    } catch (Exception e) {
      e.printStackTrace();
    }


  }
}
